// // <copyright file=StorageServiceProperties.cs company="Microsoft">
// //
// //  This source is subject to the Microsoft Public License (Ms-PL).
// //  Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// //  All other rights reserved.
// // </copyright>

namespace XRay.Library.Contracts
{
    #region

    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using XRay.Library.Helpers;

    #endregion

    [XmlRoot("StorageServiceProperties")]
    public class StorageServiceProperties
    {
        public StorageServiceProperties()
        {
        }

        public StorageServiceProperties(Logging logging, Metrics metrics)
        {
            Guard.ArgumentNotNull(logging, "logging");
            Guard.ArgumentNotNull(metrics, "metrics");

            Logging = logging;
            Metrics = metrics;
        }

        [XmlElement("Logging")]
        public Logging Logging { get; set; }

        [XmlElement("Metrics")]
        public Metrics Metrics { get; set; }

        public static StorageServiceProperties Convert(string xmlProperties)
        {
            return XmlSerializationHelper.DesirializeFromXml<StorageServiceProperties>(Encoding.UTF8, xmlProperties);
        }

        public static string Convert(StorageServiceProperties properties)
        {
            return XmlSerializationHelper.SerializeToXml(Encoding.UTF8, properties);
        }
    }

    internal static class XmlSerializationHelper
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
            "CA2202:Do not dispose objects multiple times")]
        public static T DesirializeFromXml<T>(Encoding encoding, string xmlString)
        {
            using (var buffer = new MemoryStream(encoding.GetBytes(xmlString)))
            {
                using (var reader = XmlReader.Create(buffer))
                {
                    var xmlSerializer = new XmlSerializer(typeof (T));
                    if (xmlSerializer.CanDeserialize(reader))
                    {
                        buffer.Seek(0, SeekOrigin.Begin);
                        return (T) xmlSerializer.Deserialize(buffer);
                    }
                    return default(T);
                }
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability",
            "CA2000:Dispose objects before losing scope")]
        public static string SerializeToXml<T>(Encoding encoding, T obj)
        {
            using (var buffer = new MemoryStream())
            {
                using (var writer = new XmlTextWriter(buffer, encoding))
                {
                    var xmlSerializer = new XmlSerializer(typeof (T), "");
                    var ns = new XmlSerializerNamespaces();
                    ns.Add("", "");

                    xmlSerializer.Serialize(writer, obj, ns);

                    buffer.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(buffer))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }
    }
}